package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Answer;
import cn.wolfcode.luowowo.article.domain.Ask;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Tag;
import cn.wolfcode.luowowo.article.query.AskQuery;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import cn.wolfcode.luowowo.article.service.IAskService;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITagService;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.cache.service.IQuestionRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.vo.AnswerStatisVO;
import cn.wolfcode.luowowo.cache.vo.QuestionStatisVO;
import cn.wolfcode.luowowo.comment.domain.CommunityComment;
import cn.wolfcode.luowowo.comment.service.ICommunityCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/ask")
public class AskController {
    @Reference
    private IAskService askService;

    @Reference
    private ITagService tagService;

    @Reference
    private IAnswerService answerService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private ICommunityCommentService commentService;

    @Reference
    private IQuestionRedisService redisService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private IAnswerRedisService answerRedisService;

    // 查询问题列表
    @RequestMapping("")
    public String list(Model model,@ModelAttribute("qo") AskQuery qo,@UserParam UserInfo userInfo) {
        if (userInfo != null){
            model.addAttribute("user",userInfo);
        }

        model.addAttribute("pageInfo",askService.query(qo));

        model.addAttribute("listUser",userInfoRedisService.listUserInfoAnswer());

        return "community/wenda";
    }

    @RequestMapping("/publish")
    public String publish(Model model,@UserParam UserInfo userInfo){
        if (userInfo != null){
            model.addAttribute("user",userInfo);
        }
        model.addAttribute("dests",destinationService.list());
        List<Tag> tags = tagService.listAll();

        model.addAttribute("tag",tags);
        return "community/publish";
    }

    //跳转回答问题界面
    @RequestMapping("/input")
    public String input(Model model,Long id,@UserParam UserInfo userInfo){
        if (userInfo != null){
            model.addAttribute("user",userInfo);
        }
        model.addAttribute("askId",id);
        return "community/input";
    }

    //跳转回答问题界面
    @RequestMapping("/answer/save")
    @ResponseBody
    public AjaxResult save(Model model,Answer answer,String contentId,@UserParam UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        try {
            answer.setUser(userInfo);
            answerService.save(answer,contentId);
            result.setData(answer.getAsk().getId());
        }catch (Exception e){
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }

        redisService.setCommentnum(answer.getAsk().getId());

        userInfoRedisService.setZsetVO(userInfo.getId());
        return result;
    }
    @RequestMapping("/answer/toDetail")
    public String toDetail(Model model, Long id, @UserParam UserInfo userInfo){

        if (userInfo != null){
            model.addAttribute("user",userInfo);
        }
        model.addAttribute("ask",askService.getAsk(id));
        List<Answer> answers = answerService.getAnswerById(id);
        for (Answer answer : answers) {
            AnswerStatisVO vo = answerRedisService.getAnswerStatisVo(answer.getId(), 0);
            answer.setVo(vo);
        }
        model.addAttribute("answers",answers);

        model.addAttribute("vo",redisService.setBrowsenum(id));

        return "community/wendaDetail";
    }

    @RequestMapping("/answer/commentAdd")
    @ResponseBody
    public AjaxResult commentAdd(CommunityComment comment, @UserParam UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        if(comment.getContent()!= "" && comment.getContent()!= null){
            if (userInfo!=null) {
                comment.setItselfUser(userInfo.getId());
                comment.setItselfNickname(userInfo.getNickname());
                comment.setItselfHeadImgUrl(userInfo.getHeadImgUrl());
            }
            commentService.save(comment);
        }

        return result;
    }


    @RequestMapping("/askPublish")
    @ResponseBody
    public AjaxResult publish(Model model,Ask ask, @UserParam UserInfo userInfo){

        AjaxResult result = new AjaxResult();
        try {
            ask.setUserId(userInfo);
            askService.save(ask);
            System.out.println(111);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMsg(e.getMessage());
            System.out.println(222);
        }
        return result;
    }


    //社区顶
    @RequestMapping("/askThumbup")
    @ResponseBody
    public AjaxResult askThumbup(Model model,Long id, @UserParam UserInfo userInfo){

        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }

        boolean ret = redisService.askThumbup(id,userInfo.getId());

        System.out.println(ret);

        QuestionStatisVO vo = redisService.getAskStatisVo(id, ret? 1 : 0);

        return new AjaxResult(ret,"").getData(vo);
    }

    //社区顶
    @RequestMapping("/answerThumbup")
    @ResponseBody
    public AjaxResult answerThumbup(Model model,Long id, @UserParam UserInfo userInfo){

        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }

        boolean ret = answerRedisService.answerThumbup(id,userInfo.getId());


        AnswerStatisVO vo = answerRedisService.getAnswerStatisVo(id, ret? 1 : 0);

        return new AjaxResult(ret,"").getData(vo);
    }
    //社区顶
    @RequestMapping("/answerCollect")
    @ResponseBody
    public AjaxResult answerCollect(Model model,Long id, @UserParam UserInfo userInfo){

        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }

        boolean ret = answerRedisService.answerCollect(id,userInfo.getId());


        AnswerStatisVO vo = answerRedisService.getAnswerCollecVo(id, ret? 1 : -1);

        return new AjaxResult(ret,"").getData(vo);
    }
}
